<template>
  <div class="search-container">
    <a-form
      layout="inline"
      labelAlign="left"
      style="width: 100%"
      :model="formState"
    >
      <a-row style="width: 100%" :gutter="16">
        <a-col :span="item.span" :key="item.field" v-for="item in props.form">
          <a-form-item
            :label="item.label"
            :name="item.field"
            class="left-align-label"
          >
            <a-input
              v-if="item.component === 'Input'"
              v-model:value="formState[item.field]"
              v-bind="item.componentProps"
            />
            <a-select
              v-if="item.component === 'Select'"
              v-model:value="formState[item.field]"
              v-bind="item.componentProps"
            >
              <a-option
                :label="jtem.label"
                :value="jtem.value"
                :key="jtem.value"
                v-for="jtem in item.componentProps.options"
              />
            </a-select>
            <a-date-picker
              v-if="item.component === 'DatePicker'"
              v-model:value="formState[item.field]"
              v-bind="item.componentProps"
            />
            <a-checkbox-group
              v-if="item.component === 'CheckboxGroup'"
              v-model:value="formState[item.field]"
              v-bind="item.componentProps"
            >
              <a-checkbox
                :label="jtem.value"
                :key="jtem.value"
                v-for="jtem in item.componentProps.options"
              >
                {{ jtem.label }}
              </a-checkbox>
            </a-checkbox-group>
          </a-form-item>
        </a-col>
        <a-col span="6" class="btn-group">
          <a-button type="primary" @click="handleSearch">搜索</a-button>
          <a-button type="default" @click="hanleReset" style="margin-left: 8px"
            >重置</a-button
          >
        </a-col>
      </a-row>
    </a-form>
  </div>
</template>

<script setup>
import { defineProps, ref, defineExpose } from "vue";

const props = defineProps({
  form: {
    type: Array,
    required: true,
  },
  refresh: {
    type: Function,
  },
});

const formState = ref({});

const handleSearch = () => {
  props.refresh("搜索", formState.value);
};

const hanleReset = () => {
  formState.value = {};
  props.refresh();
};

defineExpose({
  formState,
});
</script>
